<?php

class User_devices extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    
    function addDevice($user_id, $user_device_type, $user_device_token, $user_device_notifiable = '0'){
        $this->db->trans_start();
        
        $this->db->set('user_id', $user_id);
        $this->db->set('user_device_type', $user_device_type);
        $this->db->set('user_device_token', $user_device_token);
        $this->db->set('user_device_notifiable', $user_device_notifiable);
        
        $this->db->insert('user_devices');
        
        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
    function updateNotifiable($user_id, $user_device_token, $user_device_notifiable){
        $this->db->trans_start();
        
        $this->db->set('user_device_notifiable', $user_device_notifiable);
        
        $this->db->where('user_id', $user_id);
        $this->db->where('user_device_token', $user_device_token);
        
        $this->db->update('user_devices');
        
        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
    function deleteDevice($user_device_token){
        $this->db->trans_start();
        $this->db->where('user_device_token', $user_device_token);
        
        $this->db->delete('user_devices');
        
        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
    function isPaired($user_id, $user_device_token){
        $this->db->where('user_id', $user_id);
        $this->db->where('user_device_token', $user_device_token);
        $results = $this->db->get('user_devices');
        if ($results->num_rows() == 0){
            //No records
            return false;
        }
        else{
            return true;
        }
    }

}